package com.grmasa.soundtoggle;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Switch;
import android.content.Intent;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class SettingsActivity extends AppCompatActivity {

    private MySharedPreference sharedPreference;
    private LinearLayout volumeSettingLayout;
    private SeekBar musicSeekBar, ringSeekBar, notificationSeekBar, alarmSeekBar, systemSeekBar;
    private EditText musicEditText, ringEditText, notificationEditText, alarmEditText, systemEditText;
    private Switch useDefaultVolumeSwitch; // 修改变量名

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        sharedPreference = MySharedPreference.getInstance(this);

        // 初始化视图
        useDefaultVolumeSwitch = findViewById(R.id.use_default_volume);
        volumeSettingLayout = findViewById(R.id.volume_setting);
        Button openSystemVolumeBtn = findViewById(R.id.btn_open_system_volume);
        openSystemVolumeBtn.setOnClickListener(v -> {
            // 打开系统音量设置页面
            Intent intent = new Intent(android.provider.Settings.ACTION_SOUND_SETTINGS);
            startActivity(intent);
        });


        // 初始化SeekBar和EditText
        musicSeekBar = findViewById(R.id.music_seekbar);
        ringSeekBar = findViewById(R.id.ring_seekbar);
        notificationSeekBar = findViewById(R.id.notification_seekbar);
        alarmSeekBar = findViewById(R.id.alarm_seekbar);
        systemSeekBar = findViewById(R.id.system_seekbar);

        musicEditText = findViewById(R.id.music_edittext);
        ringEditText = findViewById(R.id.ring_edittext);
        notificationEditText = findViewById(R.id.notification_edittext);
        alarmEditText = findViewById(R.id.alarm_edittext);
        systemEditText = findViewById(R.id.system_edittext);

        // 设置初始状态
        boolean useDefaultVolume = sharedPreference.getBoolean("USE_DEFAULT_VOLUME", true);
        useDefaultVolumeSwitch.setChecked(useDefaultVolume);
        updateVolumeSettingVisibility(useDefaultVolume);

        // 设置SeekBar和EditText的联动
        setupSeekBarAndEditText(musicSeekBar, musicEditText, "MUSIC_VOLUME", 30);
        setupSeekBarAndEditText(ringSeekBar, ringEditText, "RING_VOLUME", 70);
        setupSeekBarAndEditText(notificationSeekBar, notificationEditText, "NOTIFICATION_VOLUME", 70);
        setupSeekBarAndEditText(alarmSeekBar, alarmEditText, "ALARM_VOLUME", 70);
        setupSeekBarAndEditText(systemSeekBar, systemEditText, "SYSTEM_VOLUME", 70);

        // 设置切换监听
        useDefaultVolumeSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            sharedPreference.putBoolean("USE_DEFAULT_VOLUME", isChecked);
            updateVolumeSettingVisibility(isChecked);
        });
    }

    private void updateVolumeSettingVisibility(boolean useDefaultVolume) {
        volumeSettingLayout.setVisibility(useDefaultVolume ? LinearLayout.VISIBLE : LinearLayout.GONE);
    }

    private void setupSeekBarAndEditText(SeekBar seekBar, EditText editText, String prefKey, int defaultValue) {
        // 从SharedPreferences加载保存的值
        int savedValue = sharedPreference.getInt(prefKey, defaultValue);
        seekBar.setProgress(savedValue);
        editText.setText(String.valueOf(savedValue));

        // SeekBar变化时更新EditText
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    editText.setText(String.valueOf(progress));
                    sharedPreference.putInt(prefKey, progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });

        // EditText变化时更新SeekBar
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().isEmpty()) {
                    try {
                        int value = Integer.parseInt(s.toString());
                        if (value >= 0 && value <= 100) {
                            seekBar.setProgress(value);
                            sharedPreference.putInt(prefKey, value);
                        } else if (value > 100) {
                            editText.setText("100");
                            seekBar.setProgress(100);
                            sharedPreference.putInt(prefKey, 100);
                        }
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}